package com.study.jdbc.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.UUID;

import com.study.jdbc.dao.CustomerDao;
import com.study.jdbc.domain.Customer;
import com.study.jdbc.factory.BeanFactory;
import com.study.jdbc.service.CustomerService;
import com.study.jdbc.utils.PageBean;

/**
* @Name: CustomerServiceImpl
* @Description: 客户信息CRUD操作Service接口实现类
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-2-16 下午8:10:25 
* @Version: V1.0
 */
public class CustomerServiceImpl implements CustomerService {
	
	private CustomerDao customerDao = BeanFactory.getCustomerDaoImpl() ;

	@Override
	public Customer findCustomerById(Serializable id) {
		return customerDao.findCustomerById(id);
	}

	@Override
	public List<Customer> findAllCustomers() {
		return customerDao.findAllCustomers();
	}

	@Override
	public void saveCustomer(Customer customer) {
		customer.setId(UUID.randomUUID().toString()) ;
		customerDao.saveCustomer(customer) ;
	}

	@Override
	public void updateCustomer(Customer customer) {
		if(customer.getId() == null || "".equals(customer.getId())) {
			throw new IllegalArgumentException("更新客户信息：主键Id不能为空！") ;
		}
		customerDao.updateCustomer(customer) ;
	}

	@Override
	public void deleteCustomerById(Serializable id) {
		customerDao.deleteCustomerById(id) ;
	}

	public PageBean<Customer> findCustomerPageRecords(String pageNum) {
		//设置当前页码，默认为第1页
		int currentPageNum = 1 ;
		if(pageNum != null && !"".equals(pageNum)) {
			currentPageNum = Integer.valueOf(pageNum) ;
		}
		//获取总记录数
		int totalRecoedsNum = customerDao.getTotalRecordsCount() ;
		PageBean<Customer> pageBean = new PageBean<Customer>(totalRecoedsNum, currentPageNum) ;
		List<Customer> pageList = customerDao.findPageRecords(pageBean.getStartIndex(), pageBean.getPageSize()) ;
		//设置分页记录
		pageBean.setRecords(pageList) ;
		return pageBean ;
	}


}
